<template>
  <div class="search-container">
    <van-nav-bar
      :title="`${$route.query.keyword} 的搜索结果`"
      left-arrow
      @click-left="$router.go(-1)"
      fixed
    />
    <van-list
      v-model="loading"
      :finished="finished"
      finished-text="没有更多了"
      @load="onLoad"
    >
      <van-cell-group>
        <van-cell
          :title="item.title"
          v-for="item in resList"
          :key="item.art_id"
          @click="
            $router.push({ path: '/article', query: { id: item.art_id } })
          "
        >
        </van-cell>
      </van-cell-group>
    </van-list>
  </div>
</template>

<script>
import { resSearch } from './home.js'
export default {
  name: 'searchres',
  data () {
    return {
      resList: [],
      loading: false,
      finished: false,
      page: 1
    }
  },
  methods: {
    async onLoad () {
      let res = await resSearch({
        q: this.$route.query.keyword,
        page: this.page
      })
      this.resList.push(...res.data.data.results)
      this.page++
      this.loading = false
      //返回的结果没有了，回返回空数组
      if (res.data.data.results.length == 0) {
        this.finished = true
      }
    }
  }
  //   async created () {
  //     let res = await resSearch(this.$route.query.keyword)
  //     console.log(res)
  //     this.resList = res.data.data.results
  //   }
}
</script>

<style lang="less">
// .search-container .van-nav-bar {
//   position: fixed;
//   width: 100%;
// }
</style>
